package com.fingard.dsp.bank.directbank.hebbank01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.dbobj.updownfile.DownFileHelper;
import com.fingard.dsp.bank.dbobj.updownfile.DownFileRecord;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.directbank.bankInterface.IReceiptPro;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2020-03-10.
 * @version 1.0
 */
public class HEBBANK01ReceiptPro extends HEBBANK01Base implements IReceiptPro {
    @Override
    public RetAtsReceipt qryReceiptOneDay(ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, Date pOneDay) throws Exception {
        String qryDay8 = Format.Date8Format.format(pOneDay);
        HEBBANK01Header bankHeader = new HEBBANK01Header();
        bankHeader.bsnCode = "CBE018";
        bankHeader.serialNo = pReqReceipt.reqHeader.transSeqID;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("ReqParam");
        xmlWriter.writeElementString("Account_num", pReqReceipt.actNum);//账号
        xmlWriter.writeElementString("receiptDate", qryDay8);//查询日期
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();

        String[] bankResult = sendToBank(xmlWriter.toXmlString(), bankHeader);
//        String[] bankResult = new String[]{"","0000||#<?xml version=\"1.0\" encoding=\"GBK\" ?><SCCBEBankData><opRep><serialNo>1903424913</serialNo><opResult><stt>0</stt><pointInfor>已受理,请等待处理</pointInfor><RepReserved1/><RepReserved2/></opResult></opRep></SCCBEBankData>"};
        if (bankResult[0].length() == 0) {
            String[] result = bankResult[1].split("\\|");
            if ("0000".equals(result[0])) {
                StringBuilder tmpSbMsg = new StringBuilder();
                String xmlStr = bankResult[1].substring(bankResult[1].indexOf("|#") + 2);
                XmlTextReader tmpBkReader = loadBankRespXmlReader(xmlStr, tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/stt");
                    String returnMsg = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/pointInfor");
                    if ("0".equals(returnCode) || "1".equals(returnCode)) {
                        DownFileRecord downRd = new DownFileRecord();
                        downRd.srcSys = this.bankFront.bankCode;
                        downRd.bank = banks;
                        downRd.bankFrontCode = this.bankFront.code;
                        downRd.busDate = Integer.parseInt(Format.Date8Format.format(pOneDay));
                        downRd.downState = "1";
                        downRd.filePath = qryDay8 + "_" + pReqReceipt.actNum + ".zip";
                        downRd.ownAct = pReqReceipt.actNum;
                        DownFileHelper dHelper = null;
                        try {
                            Date nowDay = new Date();
                            //此时采用当日，记录待下载文件，通过计划任务运行下载
                            dHelper = new DownFileHelper(pReqReceipt.reqHeader.tenant, nowDay);
                            int rowsAff = dHelper.insertIfNotExist(downRd);
                            String aLog = "写入文件下载记录表行数：" + rowsAff;
                            if (rowsAff == 1) {
                                aLog += "，记录写入成功";
                            } else if (rowsAff == 0) {
                                aLog += "，记录已存在";
                            }
                            WriteBankLogLn(aLog);
                        } catch (Exception ex) {
                            WriteBankLogLn(ex);
                        } finally {
                            if (dHelper != null) {
                                dHelper.close();
                            }
                        }
                    } else {
                        WriteBankLogLn("申请推送" + qryDay8 + "日回单文件失败,原因:" + returnMsg);
                        return null;
                    }
                } else {
                    WriteBankLogLn("申请推送" + qryDay8 + "日回单文件失败,原因:" + tmpSbMsg.toString());
                    return null;
                }
            } else {
                WriteBankLogLn("申请推送" + qryDay8 + "日回单文件失败,原因:" + result[1]);
                return null;
            }
        } else {
            WriteBankLogLn("申请推送" + qryDay8 + "日回单文件失败,原因:" + bankResult[1]);
            return null;
        }
        WriteBankLogLn("查询" + qryDay8 + "日回单成功,请等待推送数据。");
        return null;
    }
}
